package com.hnhs.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hnhs.dao.CategoryDao;
import com.hnhs.dao.ProductDao;
import com.hnhs.info.PageInfo;
import com.hnhs.model.Category;
import com.hnhs.model.CategoryQuery;
import com.hnhs.model.Product;
import com.hnhs.model.ProductQuery;
import com.hnhs.threadLocal.LocalPageInfoUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductService {

    @Resource
    private ProductDao productDao;

    public List<Product> select(ProductQuery query){
        if(query.getPageNum()!=null && query.getPageSize()!=null){
            PageHelper.startPage(query.getPageNum(),query.getPageSize());
        }

        List<Product> productList = productDao.select(query);
        if(productList instanceof Page){
            Page page =(Page) productList;
            long total = page.getTotal();
            LocalPageInfoUtil.set(PageInfo.builder().Total(total).build());
        }

        return productList;
    }
    public Integer insert(Product product){
        return productDao.insert(product);
    }
    public Integer update(Product product){
        return  productDao.update(product);
    }
    public Integer updateStatus(Integer id){
        return productDao.updateStatus(id,0);
    }
}
